$(function() {
    var cols=6;
	var rows_delete=0;
	var mode="add";
	var m_id=0;
    var url='controls/leave_model.php';
	 if (_dd==1){
		$("#btnAdd").show();
	}else{
		$("#btnAdd").hide();
	}
   	var checkDate = $('.btndate1').datepicker().on('changeDate', function(ev) {
	  checkDate.hide();
	}).data('datepicker');
	
	var checkDate2 = $('.btndate2').datepicker().on('changeDate', function(ev) {
	  checkDate2.hide();
	}).data('datepicker');
	
	var checkDate3 = $('.btndate3').datepicker().on('changeDate', function(ev) {
	  checkDate3.hide();
	}).data('datepicker');
	
	var checkDate4 = $('.btndate4').datepicker().on('changeDate', function(ev) {
	  checkDate4.hide();
	}).data('datepicker');
	
     $("#txtDateFrom,#txtDateTo,#txtDateFrom_s,#txtDateTo_s").val(_cdate);
	 LoadData(0,'',_cdate,_cdate);
	 
	 $.get(url, {"m":"ly"},function(data){
			var obj=eval('('+data+')');	
			if (obj != null){
				$.each(obj, function(index, text) {
					$("#cboLeave").append(
							$('<option></option>').val(text).html(index)
						 );
				});
			}
	});
	
	$.get(url, {"m":"emp"},function(data){
		var obj=eval('('+data+')');	
		if (obj != null){
			$.each(obj, function(index, text) {
				$("#cboEmp").append(
						$('<option></option>').val(index).html(text)
					 );
			});
		}
	});
	
	$.get(url, {"m":"l"},function(data){
			var obj=eval('('+data+')');	
			if (obj != null){
				$.each(obj, function(index, text) {
					$("#cboLeave_s").append(
							$('<option></option>').val(text).html(index)
						 );
				});
			}
	});
	$("#cboLeave_s").append($('<option></option>').val(0).html(' '));
	
	 function LoadData(type,name,df,dt){
        $( "#resultData tbody tr" ).remove();
 
        
        $('#resultData').dataTable( {
			"bProcessing": true,
			"bFilter": false,
			"bAutoWidth": false,
			"bLengthChange": false,
			"sAjaxSource": url,
			"oLanguage": {
				"sEmptyTable":     "ไม่พบข้อมูล"
			},
			"bDestroy": true,
			"sServerMethod": "POST",
			"fnServerParams": function ( aoData ) {
					aoData.push( { "name": "m", "value": "load"} ,
					{ "name": "type", "value": type} ,
					{ "name": "name", "value": name} ,
					{ "name": "datefrom", "value": df} ,
					{ "name": "dateto", "value": dt} 
						);
				},
			"aoColumnDefs":[
				{  "bVisible": false , "aTargets": [ 1 ] },	
				{   "bSortable": true, "aTargets": [ 2 ] ,"sClass":"center"},	
				{   "bSortable": true, "aTargets": [ 3 ] ,"sClass":"center" },	
				{   "bSortable": true, "aTargets": [ 4 ]  },	
				{   "bSortable": true, "aTargets": [ 5 ]  },	 
				 
				{"aTargets": [ 6 ],
					"sClass":"center",
					"mData": null,
					"mRender": function ( data, type, full ) {
						if(_te==1)
						return "<a href='#popupConfirm' data-toggle='modal'><img src='img/delete-file.png' class='btnDel' style='cursor:pointer;'/></a>";
						else 
						return "";
					}
				}
			],"fnDrawCallback": function ( oSettings ) {
				 var that = this;
				/* Need to redo the counters if filtered or sorted */
                 var iTotalRecords = oSettings.fnRecordsTotal();
				//alert(oSettings.aiDisplay.length + ' ' + iTotalRecords);
				 
					if (iTotalRecords > 0){ 	
						if ( oSettings.bSorted || oSettings.bFiltered )
						{
							for ( var i=0, iLen=oSettings.aiDisplay.length ; i<iLen ; i++ )
							{
								$('td:eq(0)', oSettings.aoData[ oSettings.aiDisplay[i] ].nTr ).html( i+1 );
							}
						}
					}
					 
				} 
		 } );
		 
      }
 	
	 
	$("#btnSearch").click(function(){
		var _type = $("#cboLeave_s").val();
		var _name = $("#txtName_s").val();
		var _df = $("#txtDateFrom_s").val();
		var _dt = $("#txtDateTo_s").val();
		
		LoadData(_type,_name,_df,_dt);
	});
	
	$("#btnSave").live('click',function(){
		$("#txtDateFrom,#txtDateTo, #txtReason").jqBootstrapValidation({preventSubmit: false,
			submitSuccess: function ($form, event) { event.preventDefault();}
		}); 
	  
		var _df = $.trim($("#txtDateFrom").val());		
		var _dt = $.trim($("#txtDateTo").val());	
		var _leave = $("#cboLeave").val();
		var _reason = $("#txtReason").val();
		var _emp = $("#cboEmp").val();
		var dataSet;
		 
		if (mode=="add"){
			dataSet={"m":"add","emp":_emp,"df":_df,"dt":_dt,"leave":_leave,"reason":_reason};  
		} 
		if (_df!="" && _dt !="" && _reason !=''){
			$.ajax({
				type: 'POST',
				cache: false,
				url: url,
				data: dataSet,
				success: function(data){
					$("#popup").modal('hide');
					 $("#btnSearch").click();
					//$('#resultData').dataTable().fnReloadAjax();
				}
			});
			
		}
		 $("#txtDateFrom,#txtDateTo, #txtReason").jqBootstrapValidation("destroy");
	});		
	 
	$("#btnAdd").live('click', function(){
		mode="add";
		
		$("#txtDateFrom,#txtDateTo, #txtReason").val('');
	});
		
	//Popup confirm delete
	$(".btnDel").live('click',function(){
		var rows = $(this).closest("tr").get(0);
		rows_delete = $('#resultData').dataTable().fnGetPosition(rows);

	}); 
	
	$("#btnDelete").live('click', function(){
		var row_id =$('#resultData').dataTable().fnGetData(rows_delete);
		
		var id=row_id[1];
		 
		 $.ajax({
				 type: 'POST',
				 cache: false,
				 url: url,
				 data: {"m":"del","id":id,"tb":"master_fee"},
				 success: function(data){
					$("#popupConfirm").modal('hide');
					$('#resultData').dataTable().fnDeleteRow( rows_delete );
					$('#resultData').dataTable().fnDraw();
				 }
			 });
 
	}); 
	
	 
	$("#btnPrintDraft").click(function(){
	  $("#resultData").printThis();
		return false;
           
	});
    
});


